from django.db import models
from django.contrib.auth import  get_user_model
from apps.login.models import StormUser

# Create your models here.

#定义用户模型
User=get_user_model()

class Product(models.Model):
	#产品类型
	ProductType=(
		('WEB','WEB'),
		('APP','APP')
	)
	name=models.CharField(max_length=50,verbose_name='产品名称')
	product_type=models.CharField(max_length=50,verbose_name='产品类型',choices=ProductType)
	version=models.CharField(max_length=50,verbose_name='产品版本')
	master=models.CharField(max_length=50,verbose_name='产品负责人')
	description=models.CharField(max_length=1024,verbose_name='产品详细描述')
	last_update_time=models.DateTimeField(auto_now=True,verbose_name='最近修改时间')
	create_time=models.DateTimeField(auto_now_add=True,verbose_name='创建时间')

	class Meta:
		verbose_name='产品信息'

class Host(models.Model):
	name=models.CharField(max_length=50,verbose_name='环境名称')
	host=models.CharField(max_length=50,verbose_name='环境地址')
	description=models.CharField(max_length=200,verbose_name='环境描述')
	product=models.ForeignKey(Product,on_delete=models.CASCADE,verbose_name='所属产品',related_name='host_list')

	class Meta:
		verbose_name='环境管理信息'

HTTP_METHOD_CHLICE=(
	('GET','GET'),
	('POST','POST'),
	('PUT','PUT'),
	('DELETE','DELETE')
)

STATUS_CODE_CHOICE=(
	('200','200'),
	('201','201'),
	('204','204'),
	('301','301'),
	('302','302'),
	('400','400'),
	('401','401'),
	('404','404'),
	('405','405'),
	('500','500'),
	('502','502')
)

class Api(models.Model):
	name=models.CharField(max_length=50,verbose_name='API名称')
	http_method=models.CharField(max_length=50,verbose_name='接口方法',choices=HTTP_METHOD_CHLICE)
	host=models.ForeignKey(Host,on_delete=models.CASCADE,verbose_name='请求地址')
	path=models.CharField(max_length=50,verbose_name='路由地址')
	headers=models.TextField(null=True,blank=True,verbose_name='请求头')
	data=models.TextField(null=True,blank=True,verbose_name='请求参数')
	expect_code=models.CharField(default=200,max_length=50,verbose_name='期望状态码',choices=STATUS_CODE_CHOICE)
	expect_content=models.CharField(null=True,blank=True,max_length=2000,verbose_name='响应内容')
	product=models.ForeignKey(Product,on_delete=models.CASCADE,verbose_name='所属产品',null=True,related_name='api_list')
	#该参数主要应用来做性能测试使用
	count=models.IntegerField(default=10,verbose_name='并发次数')
	description=models.CharField(max_length=500,verbose_name='API描述信息')

	class Meta:
		verbose_name='单个API的信息'



class ApiRunRecord(models.Model):
	url=models.CharField(max_length=200,verbose_name='请求地址')
	http_method=models.CharField(max_length=10,verbose_name='请求方法',choices=HTTP_METHOD_CHLICE)
	data=models.TextField(null=True,verbose_name='请求参数')
	headers=models.TextField(null=True,verbose_name='请求头')
	# user=models.ForeignKey(StormUser,on_delete=models.CASCADE,verbose_name='执行者')
	return_code=models.CharField(max_length=10,verbose_name='状态码')
	return_content=models.TextField(null=True,verbose_name='响应内容')
	return_time=models.CharField(max_length=100,verbose_name='响应时间')
	api=models.ForeignKey(Api,on_delete=models.CASCADE,verbose_name='被执行的API',null=True)
	create_time=models.DateTimeField(auto_now=True,verbose_name='运行时间')

	class Meta:
		#根据时间进行倒序排序,并且只显示最后一次结果的信息
		ordering=['-create_time']
		verbose_name='API运行记录'

class RunApiPermod(models.Model):
	responseTime=models.CharField(max_length=20,verbose_name='执行响应时间')
	throughput=models.CharField(max_length=20,verbose_name='吞吐量')
	rate=models.CharField(max_length=100,verbose_name='平均响应时间')
	# timeData=models.TextField(verbose_name='响应时间信息')
	maxTime=models.CharField(max_length=100,verbose_name='最大响应时间')
	minTime=models.CharField(max_length=100,verbose_name='最小响应时间')
	media=models.CharField(max_length=100,verbose_name='中位数')
	percent90=models.CharField(max_length=100,verbose_name='90%Line')
	percent95=models.CharField(max_length=100,verbose_name='95%Line')
	percent99=models.CharField(max_length=100,verbose_name='99%Line')
	errorRate=models.CharField(max_length=100,verbose_name='错误率')
	sum=models.IntegerField(verbose_name='请求总数')
	fails=models.IntegerField(verbose_name='失败数')
	api=models.ForeignKey(Api,on_delete=models.CASCADE,verbose_name='关联的API',null=True)
	create_time=models.DateTimeField(auto_now=True,verbose_name='运行时间')
	class Meta:
		#根据时间进行倒序排序,并且只显示最后一次结果的信息
		ordering=['-create_time']
		verbose_name='API并发请求次数'


class Suite(models.Model):
	product=models.ForeignKey(Product,on_delete=models.CASCADE,verbose_name='所属产品',related_name='suite_list')
	name=models.CharField(max_length=50,verbose_name='测试套件名称')
	api_list=models.ManyToManyField(Api,related_name='suite_list')
	description=models.CharField(max_length=1024,null=True,blank=True,verbose_name='详细描述信息')
	create_time=models.DateTimeField(auto_now=True,verbose_name='更新时间')

	class Meta:
		verbose_name='测试套件'

class SuiteArgument(models.Model):
	'''这些全局参数是针对所有的测试套件的'''
	suite=models.ForeignKey(Suite,on_delete=models.CASCADE,verbose_name='测试用例',null=True,related_name='arguments')
	name=models.CharField(max_length=100,verbose_name='参数名字')
	value=models.CharField(max_length=100,verbose_name='参数的值')

	class Meta:
		verbose_name='全局参数'


class ApiArgument(models.Model):
	ARGUMENT_ORIGIN_CHOICE=(
		('HEADER','HEADER'),
		('BODY','BODY'),
		('COOKIE','COOKIE')
	)
	api=models.ForeignKey(Api,on_delete=models.CASCADE,verbose_name='用例API',null=True,related_name='arguments')
	name=models.CharField(max_length=100,verbose_name='参数名字')
	origin=models.CharField(max_length=100,choices=ARGUMENT_ORIGIN_CHOICE,verbose_name='参数来源')
	format=models.CharField(max_length=100,verbose_name='参数的格式')

	class Meta:
		verbose_name='API参数'

class SuiteRunRecord(models.Model):
	suite=models.ForeignKey(Suite,on_delete=models.CASCADE,verbose_name='所属套件')
	create_time=models.DateTimeField(auto_now=True,verbose_name='执行时间')

	class Meta:
		verbose_name='套件运行记录'
		ordering=['-create_time']

class SuiteApiRunRecord(models.Model):
	url=models.CharField(max_length=200,verbose_name='请求地址')
	http_method=models.CharField(max_length=10,verbose_name='请求方法',choices=HTTP_METHOD_CHLICE)
	headers=models.TextField(null=True,verbose_name='请求头')
	data=models.TextField(null=True,verbose_name='请求参数')
	return_code=models.CharField(max_length=10,verbose_name='返回状态码')
	return_content=models.TextField(null=True,verbose_name='响应内容')
	api=models.ForeignKey(Api,on_delete=models.CASCADE,verbose_name='关联的API')
	suite_record=models.ForeignKey(SuiteRunRecord,on_delete=models.CASCADE,verbose_name='关联的测试套件',related_name='suite_records')
	create_time=models.DateTimeField(auto_now=True,verbose_name='运行的时间')

	class Meta:
		verbose_name='测试套件里面的API运行记录'
		ordering=['-create_time']


